package office.service.system.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import office.bo.system.Department;
import office.domainset.BizDomainProviderSetImpl;
import office.service.system.ISystemService;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;

public class SystemService extends BaseBizServiceImpl implements ISystemService {

	@Override
	public void saveDepartment(String parentId ,Department department) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			if(department != null && department.getId() != null && !department.getId().equals("")){
				Department newDepartment = new Department();
				newDepartment = getDepartmentById(department.getId().toString());
				newDepartment.setName(department.getName());
				newDepartment.setMemo(department.getMemo());
				newDepartment.store(domainProviderSetImpl);
			}else{
				if(parentId != null && parentId.equals("")){
					parentId = null;
				}
				Serializable s = department.insertIntoDB(parentId, domainProviderSetImpl);
				domainProviderSetImpl.getDao().getMySession().flush();
				Department newDepartment = new Department();
				newDepartment = getDepartmentById(s.toString());
				if(department != null && department.getType().equals("department")){
					Department upDepartment = new Department();
					upDepartment = getParentFor(newDepartment);
					newDepartment.setCompanyId(upDepartment.getId().toString());
					newDepartment.setCompanyName(upDepartment.getName());
				}else{
					newDepartment.setCompanyId(newDepartment.getId().toString());
					newDepartment.setCompanyName(newDepartment.getName());
				}
				newDepartment.store(domainProviderSetImpl);
			}
		} catch (BusinessException e) {
			log.error("", e);
		}
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getAllDepartment(){
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IDomainProvider departmentDP = domainProviderSetImpl.getDepartmentDP();
			List<Department> list = (List<Department>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(departmentDP, "allNode", new HashMap());
			return list;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}


	@Override
	public void deleteDepartmentById(String id) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().delTreeNodeById(Department.class, id);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 根据部门ID 获取部门信息
	 */
	@Override
	public Department getDepartmentById(String id) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IDomainProvider departmentDP = domainProviderSetImpl.getDepartmentDP();
			Department department = (Department)domainProviderSetImpl.getDao().findByIdAndDomain(departmentDP, id);
			return department;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	/**
	 * 根据部门ID 获取所有的子部门
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDescendants(String id) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Department department = getDepartmentById(id);
			List<Department> listd = department.getDescendants(domainProviderSetImpl);
			return listd;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
	
	/**
	 * 根据部门ID 获取所有的子部门
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getDescendantsId(String id) {
		try {
			List<Object> l = new ArrayList<Object>();
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Department department = getDepartmentById(id);
			List<Department> listd = department.getThisAndDescendants(domainProviderSetImpl);
			for (Iterator iterator = listd.iterator(); iterator.hasNext();) {
				Department department2 = (Department) iterator.next();
				l.add(department2.getId());
			}
			return l;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	/**
	 * 根据部门ID获取本部门的父节点
	 */
	@Override
	public Department getParent(String id) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Department department = getDepartmentById(id);
			if(department != null && department.getType().equals("group")){
				return null;
			}
			return (Department)department.getParent(domainProviderSetImpl);
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}


	@Override
	public Department getParentFor(Department newDepartment) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Department department = (Department)newDepartment.getParent(domainProviderSetImpl);
			if(department != null && department.getType().equals("department")){
				return getParentFor(department);
			}else{
				return department;
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	/**
	 * 首先根据本部门ID 获取上级部门，
	 * 如果上级部门类型为子公司或者集团，则根据传入的公司ID查询所有的子部门，
	 * 如果上级部门是部门类型则继续根据部门ID查询所有的子部门
	 * 
	 * 此方法用在 审批流程中，当一个子公司没有对应的部门的时候，系统会自动的向上查找对应部门。
	 */
	@Override
	public List<Object> getDepartment(String departmentId,String companyId) {
		List<Object> l = new ArrayList<Object>();
		Department d = getParent(departmentId);
		List <Department> listd = null;
		if(d != null){
			if(d.getType().equals("group")||d.getType().equals("ccompany")){
				listd = getDescendants(companyId);
				l.add(companyId);
			}else{
				listd = getDescendants(departmentId);
				l.add(departmentId);
			}
		}else{
			listd = getDescendants(departmentId);
			l.add(departmentId);
		}
		if(listd != null && listd.size()>0){
			for (Iterator<Department> iterator = listd.iterator(); iterator.hasNext();) {
				Department department = (Department) iterator.next();
				l.add(department.getId());
			}
		}
		return l;
	}
	

}
